<?php

class NewsController extends Controller
{
	public function actionDetail($id)
	{
		$model = News::model()->findByPk($id);
      if(is_null($model))
         $this->redirect(Yii::app()->request->baseUrl.'/tin-tuc');
      $model->news_view++;
      $model->save();
      $current_url = Yii::app()->request->hostInfo.'/'.$model->linkDetail($id,$model->news_title);
      $this->pageTitle = $model->news_title;
      $this->render('detail',array(
         'model' => $model,
         'current_url' => $current_url,
      ));
	}

	public function actionIndex()
	{
		$limit = 6;
      $page = Yii::app()->request->getParam('page',1);
      $tag = Yii::app()->request->getParam('tag','');
      $offset = $limit*$page - $limit;
      $criteria = new CDbCriteria();
      $criteria->offset = $offset;
      $criteria->limit = $limit;
      $criteria->order = 'news_id DESC';
      if($tag != ''){
         $criteria->addCondition('news_tags LIKE \'%' . $tag . '%\'');
      }
      $model = new News();
      $list = $model->findAll($criteria);
      $count = $model->count($criteria);
      $pagination = new Zebra_Pagination();
      $pagination->records($count);
      $pagination->records_per_page($limit);
      $pagination->selectable_pages(5);
      $pagination->labels('Trước','Sau');     
      $this->layout = 'shop';
      $this->render('index',array(
         'model' => $model,
         'list' => $list,
         'pagination' => $pagination,
      ));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}